package com.yqya.springboot.email.server.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.yqya.springboot.email.server.dto.EmailDTO;
import com.yqya.springboot.email.server.service.EmailService;
import com.yqya.springboot.email.server.vo.EmailVO;

/**
 * 邮件
 *
 * @author Kevin
 * @date 2019-06-30
 */
@RestController
public class EmailController {

    @Autowired
    private EmailService emailService;

    /**
     * 发送邮件的主体界面
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ModelAndView index() {
        // 打开发送邮件的页面
        ModelAndView mv = new ModelAndView("email/sendEmail");
        mv.addObject("from", emailService.getEmailSendFrom());
        return mv;
    }

    /**
     * 发送邮件
     *
     * @param dto
     * @return
     */
    @RequestMapping(value = "email/send", method = RequestMethod.POST)
    public EmailVO sendMsgToUser(EmailDTO dto, MultipartFile[] files) {
        dto.setMultipartFiles(files);
        // 发送邮件和附件
        EmailVO vo = emailService.sendEmail(dto);
        return vo;
    }

}
